package com.jingxi.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.portal.pojo.CartItem;
import com.jingxi.portal.service.CartService;

@Controller
@RequestMapping("/cart")
public class CartController {

	@Autowired
	private CartService cartService;

	@RequestMapping(value="/add/{itemId}")
	public String addItemToCart(@PathVariable long itemId,@RequestParam(defaultValue="1")Integer num,
			HttpServletRequest request,
			HttpServletResponse response) {
		cartService.addCartItem(itemId, num, request, response);
		return "redirect:/success";
	}

	@RequestMapping("/success")
	public String goCartSuccess() {
		return "cartSuccess";
	}

	@RequestMapping("/cart")
	public String showCart(Model model, HttpServletRequest request) {
		List<CartItem> list = cartService.getCartItemList(request);
		model.addAttribute("cartList", list);
		return "cart";
	}

	@RequestMapping("/delete/{itemId}")
	public String removeItemFromCart(@PathVariable long itemId, HttpServletRequest request,
			HttpServletResponse response) {
		cartService.deleteCartItem(itemId, request, response);
		return "redirect:/cart/cart.html";
	}

}
